﻿Imports System.Data.SqlClient
Imports System.IO.Ports

Public Class recieve
    Public MainForm As Form1
    Dim MyEngine As New engine
    Dim index As String = ""
    Sub LoadInitialData()
        Dim constring As String = My.Settings.connectionstring
        Using con As New SqlConnection(constring)
            Using cmd As New SqlCommand("SELECT * FROM [recieved] order by sms_date asc", con)
                cmd.CommandType = CommandType.Text
                Using sda As New SqlDataAdapter(cmd)
                    Using dt As New DataTable()
                        sda.Fill(dt)
                        For Each item As DataRow In dt.Rows
                            Dim dbindex, Sender, time, message, stats, desc, extra As String
                            dbindex = item(0)
                            Sender = item(1)
                            time = item(2)
                            message = item(3)
                            If IsDBNull(item(4)) Then
                                stats = ""
                            Else
                                stats = item(4)
                            End If
                            If IsDBNull(item(5)) Then
                                desc = ""
                            Else
                                desc = item(5)
                            End If
                            If IsDBNull(item(6)) Then
                                extra = ""
                            Else
                                extra = item(6)
                            End If
                            With ListView1.Items.Add(dbindex)
                                .SubItems.AddRange(New String() {Sender})
                                .SubItems.AddRange(New String() {time})
                                .SubItems.AddRange(New String() {message})
                            End With
                        Next

                    End Using
                End Using
            End Using
        End Using
    End Sub
    Sub LoadNewMessages()
        Dim constring As String = My.Settings.connectionstring
        Using con As New SqlConnection(constring)
            Using cmd As New SqlCommand("SELECT * FROM [recieved] order by sms_date", con)
                cmd.CommandType = CommandType.Text
                Using sda As New SqlDataAdapter(cmd)
                    Using dt As New DataTable()
                        sda.Fill(dt)
                        'Add New Row
                        For Each item As DataRow In dt.Rows
                            Dim dbindex, Sender, time, message, stats, desc, extra As String
                            dbindex = item(0)
                            Sender = item(1)
                            time = item(2)
                            message = item(3)
                            If IsDBNull(item(4)) Then
                                stats = ""
                            Else
                                stats = item(4)
                            End If
                            If IsDBNull(item(5)) Then
                                desc = ""
                            Else
                                desc = item(5)
                            End If
                            If IsDBNull(item(6)) Then
                                extra = ""
                            Else
                                extra = item(6)
                            End If
                            If CheckDuplicate(Sender & time) = False Then
                                With ListView1.Items.Add(dbindex)
                                    .SubItems.AddRange(New String() {Sender})
                                    .SubItems.AddRange(New String() {time})
                                    .SubItems.AddRange(New String() {message})
                                End With
                                'Forward to mail
                                
                            End If
                        Next
                    End Using
                End Using
            End Using
        End Using
    End Sub
    Function CheckDuplicate(key As String) As Boolean
        Try
            For Each item As ListViewItem In ListView1.Items
                Dim myKey As String = item.SubItems(1).Text & item.SubItems(2).Text
                If myKey = key Then
                    Return True
                End If
            Next
            Return False
        Catch ex As Exception
            Return True
        End Try
        Return True
    End Function

    Private Sub recieve_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadInitialData()
        Timer1.Interval = Integer.Parse(My.Settings.GetRecievedHearbeatInterval.ToString)
        Timer1.Enabled = True
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        LoadNewMessages()
        ' MsgBox("load new")
    End Sub
    Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
        Try
            txtindex.Text = ListView1.FocusedItem.SubItems(0).Text
            txtcontact.Text = ListView1.FocusedItem.SubItems(1).Text
            txtsent.Text = ListView1.FocusedItem.SubItems(2).Text
            txtmessage.Text = ListView1.FocusedItem.SubItems(3).Text
            With ListView1.FocusedItem
                index = .SubItems(0).Text
            End With
        Catch ex As Exception

        End Try
    End Sub

    Private Sub ListView1_MouseClick(sender As Object, e As MouseEventArgs) Handles ListView1.MouseClick
        If e.Button = MouseButtons.Right Then
            If ListView1.FocusedItem.Bounds.Contains(e.Location) = True Then
                ContextMenuStrip1.Show(Cursor.Position)
            End If
        End If
    End Sub

    Private Sub ReplyToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ReplyToolStripMenuItem.Click
        Dim NewMessageDialog As New sendmessage
        NewMessageDialog.txtrecipient.Text = ListView1.FocusedItem.SubItems(1).Text
        NewMessageDialog.txtmessage.Focus()
        If NewMessageDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            'Disable Recieving
            MainForm.Timer1.Enabled = False
            'Send Message
            For Each item In NewMessageDialog.ListofRecipient
                'Progress Show
                MyEngine.SendMessage(item, NewMessageDialog.Message)
            Next
        End If
        'Enable Recieving
        MainForm.Timer1.Enabled = True
    End Sub
End Class
